Oa.controller('essayCtrl',function($scope,$http,$stateParams){

	$scope.totalPage = 1;//全部页数
	$scope.currentPage = 1;//当前页码
	$scope.pageArr;// 页码数组
	$scope.size = 10;
	// 生成页码数组
	$scope.getPage = function(num){
		$scope.pageArr = new Array();
		for(var i=0;i<num;i++){
			$scope.pageArr[i] = i+1;
		}
	}
	//获取   第n页的文章
	$scope.getEssay = function(num){
		if(num>=1 && num<=$scope.totalPage){
				$http.get(getAllActivity_url,
					{
						params:{
							page:num,
							size:$scope.size
						}
					})
				.success(function(res){
					if (res.code==200) {
						$scope.essays = res.data.list;
						$scope.totalPage = 1;
						$scope.currentPage = 1;
						$scope.getPage($scope.totalPage);
						$scope.recordsnum = res.data.recordsNum;
						for (var i = 0; i < res.data.list.length; i++) {
							$scope.essays[i].cover="http://localhost:8080/"+res.data.list[i].cover;
							var d = new Date(res.data.list[i].date);
							$scope.essays[i].date = d.toLocaleString();
						}
					}else {
						Notify(res.msg,"top-right");
					}
				}).error(function(){
					Notify('客户端错误',"top-right");
			});
		}
	}
	$scope.getRecord = function(){
		$scope.getEssay($scope.currentPage);
	}
	$scope.getRecord();


	//删除团建文章
	$scope.showModal = false;
	$scope.delEssay = function(id) {
		$scope.showModal = !$scope.showModal;
		$scope.delEssayId = id;
	};
	$scope.sureDelEssay = function() {
		$http.post(delActivity_url, {
				teamActivityId: $scope.delEssayId
			})
			.success(function(res) {
				if (res.code==200) {
					$scope.getRecord();
					$scope.getCarsoule();
				}
				Notify(res.msg,"top-right");
			}).error(function() {
				Notify("客户端错误","top-right");
		});
		$scope.showModal = !$scope.showModal;
	}

	
	
	$scope.getCarsoule = function(){
		//获取轮播图内容
		$http.get(getAllActivity_url,{
			params:{
				page:1,
				size:5
			}
		})
		.success(function(res){
			if (res.code==200) {
				$scope.slider = res.data.list;
				for (var i = 0; i < res.data.list.length; i++) {
					$scope.slider[i].src="http://localhost:8080/"+res.data.list[i].cover;
				}
				console.log($scope.slider);
			}
		})
		.error(function(){});
	}
	$scope.getCarsoule();
});


Oa.controller('showessayCtrl',function($scope,$http,$stateParams,$sce){
	$http.get(getActivityById_url,{
		params:{
			teamActivityId:$stateParams.id
		}
	})
	.success(function(res){
		if(res.code==200){
			$scope.title = res.data.title;
			$scope.myText = $sce.trustAsHtml(res.data.info);
		}
		else{
			Notify(res.data,"top-right");
		}
	})
	.error(function(){
		Notify("客户端错误","top-right");
	});
});



//发表团建文章，修改团建文章
Oa.controller('editEssayCtrl',function($scope,$http,$stateParams){
	$scope.essay = {
		title:null,
		cover:null,
		info:null
	}

	//根据文章Id获取文章
	$scope.getEssayById = function(){
		$http.get(getActivityById_url,{
			params:{
				teamActivityId:$stateParams.id
			}
		})
		.success(function(res){
			if (res.code==200) {
				$scope.essay = res.data;
				editor.txt.html(res.data.info);
				// Notify(res.msg,"top-right");
			}else {
				Notify(res.msg,"top-right");
			}
		})
		.error(function(){
			Notify("客户端错误","top-right");
		})
	}
	if ($stateParams.id) {
		console.log("修改文章");
		$scope.getEssayById();
	}
	else{
		console.log("添加文张");
	}

	//发表文章
	$scope.addEssay = function() {
		var url = addTeamActivity_url;
		var formData = new FormData($( "#essay-form" )[0]);

		formData.append("info",editor.txt.html());
		formData.append("outline",editor.txt.text().substr(0,255));
		var date = new Date();
		var datastring = date.toLocaleString();
		formData.append("date",datastring);
		var xhr = new XMLHttpRequest();
		if ($stateParams.id) {
			url = updateTeamActivity_url;
			formData.append("teamActivityId",$stateParams.id);
			console.log("修改文章");
		}
		xhr.onload=function(event)
		{
			if  (( xhr.status >= 200 && xhr.status < 300) || xhr.status == 304)   //上传成功
			{
				var obj = JSON.parse(event.target.response);
				Notify(obj.msg,"top-right");
				if (obj.code==200) {
					editor.txt.clear();
					$scope.essay = {
						title:null,
						cover:null,
						info:null
					}
					location.href="http://localhost:8080/graduation/OAsystem/index.html#/teamactivity";
				}
			}
			else
			{
				var obj = JSON.parse(event.target.response);
				Notify(obj.msg,"top-right");
			}
		};

		xhr.open("post", url ,true);
		xhr.send(formData);
	}
});
